#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>

int main()
{

    if (access("/home/gec/fifo1", F_OK) == -1)
    {
        int ret = mkfifo("/home/gec/fifo1", 0777);
        printf("ret1 = %d\n", ret); // 0 成功 -1 失败
    }
    else
    {
        printf("fifo1 is exist\n");
    }

    if (access("/home/gec/fifo2", F_OK) == -1)
    {
        int ret = mkfifo("/home/gec/fifo2", 0777);
        printf("ret2 = %d\n", ret); // 0 成功 -1 失败
    }
    else
    {
        printf("fifo2 is exist\n");
    }

    int fd = open("/home/gec/fifo1", O_RDWR);
    int fd2 = open("/home/gec/fifo2", O_RDWR);

    int pid = fork();

    if (pid == 0)
    {
        while (1)
        {
            char buf[32] = {0};
            read(fd, buf, sizeof(buf));
            printf("进程1 buf = %s\n", buf);
        }
    }

    if (pid > 0)
    {
        while (1)
        {
            char buf[32] = {0};
            scanf("%s", buf);
            write(fd2, buf, strlen(buf));
        }
    }

    close(fd);
    return 0;
}